<?php
class Public_Controller extends CI_Controller
{
    public function __construct() {
        parent::__construct();
        $this->_load();
    }
    
    public function fill_phrase($sview, array $adata_view,array $aphrase = null)
    {
        parent::fill_phrase($sview, $adata_view, $aphrase);
    }
    
    private function _load() {
//        $this->_adata['captcha'] = $this->create_captcha();
        $this->config->set_item('language', $this->_slang_name);
        $this->lang->load('public/login', $this->_slang_name);
    }
    
    public function create_captcha() {
        $aConfig = array(
            'word' => random_string('alnum', 6),
            'img_path' => APPPATH . 'static/images/captcha/',
//            'font_path' => '',
            'img_url' => base_url() . APPPATH . 'static/images/captcha/',
            'img_height' => 50,
            'img_width' => 120,
            'expiration' => 7200
        );

        $this->load->helper('captcha');
        $aCaptcha = create_captcha($aConfig);
        $this->session->set_userdata($this->config->item('captcha_name'), $aCaptcha);
        
        return $aCaptcha;
    }
    
    public function check_captcha(array $aUser = null) {
        if(empty($aUser)){
            return false;
        }
        
        
        $capcha = $this->session->userdata($this->config->item('captcha_name'));
        $capcha = $capcha['word'];
        
        if ($aUser['captcha'] != $capcha) {
            return false;
        }
        
        return true;
    }
    
    public function refresh_captcha(){
        if(!$this->input->is_ajax_request()){
            return false;
        }
        
        if((bool)$aCaptcha = $this->create_captcha()){
            $this->return_success_json($aCaptcha, '');
            return true;
        }
        
        $this->return_fail_json($aCaptcha['image'], '');
    }
    
    protected function _redirect_index(){
        redirect("product/list_product/");
    }
}
?>
